package com.lnsf.bookstore.web.address.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.lnsf.bookstore.common.annotation.LoginUser;
import com.lnsf.bookstore.common.controller.ApiBaseAction;
import com.lnsf.bookstore.web.address.service.ApiAddressService;
import com.lnsf.bookstore.web.address.vo.ApiAddress;
import com.lnsf.bookstore.web.advertise.vo.ApiAdvertise;
import com.lnsf.bookstore.web.user.vo.ApiUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/address")
public class ApiAddressController extends ApiBaseAction{

    @Autowired
    private ApiAddressService apiAddressService;

    /**
     * 查看用户地址列表
     */
    @RequestMapping(value = "/list",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Object queryByUid(@LoginUser ApiUser apiUser) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId",apiUser.getUserId());
        List<ApiAddress> list = apiAddressService.queryByUid(params);
        return toResponsSuccess(list);
    }
    /**
     * 地址详情
     */
    @RequestMapping(value = "/detail",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Object detail(Integer id) {
        ApiAddress apiAddress = apiAddressService.queryByObject(id);
        return toResponsSuccess(apiAddress);
    }

    /**
     * 添加，修改用户地址
     */
    @RequestMapping(value = "/save",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Object save(@RequestBody  ApiAddress apiAddress) {
        //设置省市县信息
        if (StringUtils.isNotBlank(apiAddress.getAreaInfoCode())) {
            System.out.println(apiAddress.getAreaInfoCode());
            String[] addressCode = apiAddress.getAreaInfoCode().split(",");
            apiAddress.setProvince(addressCode[0]);
            apiAddress.setCity(addressCode[1]);
            apiAddress.setArea(addressCode[2]);
        }
        if(apiAddress.getId()!=null){  //修改
            apiAddressService.update(apiAddress);
        }else {                   //新增
            //设置用户id，测试要用，
            apiAddress.setUserId(this.getUserId());
            apiAddressService.save(apiAddress);
        }
        return toResponsSuccess(apiAddress);
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Object delete() {
        JSONObject jsonObject = this.getJsonRequest();
        apiAddressService.delete(jsonObject.getInteger("id"));
        return toResponsSuccess("");
    }

	
	
	

	
}
